﻿#include "UserSystem.h"
#include <qaction.h>
#include <Constants.h>
#include <qmessagebox.h>
#include <LoginSystem.h>
#include <UserInformation.h>
#include <Purchase_Ticket_Search.h>
#include <Global_Class.h>
#include "OrderHistoryDialog.h"
#include <QToolButton> // 修正：添加QToolButton头文件
using namespace Constants;
UserSystem::UserSystem(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	SetupUserToolBar();
}

UserSystem::~UserSystem()
{}

void UserSystem::SetupUserToolBar()
{
    ui.mainToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    // 移除原有的用户中心按钮（QAction），防止重复
    QList<QAction*> actions = ui.mainToolBar->actions();
    for (QAction* act : actions) {
        if (act == ui.UserCenterToolAction) {
            ui.mainToolBar->removeAction(act);
            break;
        }
    }

    // 创建 QToolButton 作为“用户中心”按钮
    QToolButton* userCenterButton = new QToolButton(this);
    userCenterButton->setText(ui.UserCenterToolAction->text());
    userCenterButton->setDefaultAction(ui.UserCenterToolAction);
    userCenterButton->setMenu(ui.userCenterMenu);
    userCenterButton->setPopupMode(QToolButton::InstantPopup); // 设置为InstantPopup，点击按钮即可弹出菜单
    userCenterButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    ui.mainToolBar->addWidget(userCenterButton);

    // 其它功能按钮仍用 addAction
    ui.mainToolBar->addAction(ui.RemainingTicketsSearchToolAction);
    ui.mainToolBar->addAction(ui.PurchaseTicketsToolAction);
    ui.mainToolBar->addAction(ui.TicketRefundOrChangeToolAction);
    ui.mainToolBar->addAction(ui.OrderHistorySearchToolAction);

    // 连接所有action信号
    connect(ui.userInfoAction, &QAction::triggered, this, &UserSystem::onUserInformationAction);
    connect(ui.logoutAction, &QAction::triggered, this, &UserSystem::onLogoutAction);
    connect(ui.userDeleteAction, &QAction::triggered, this, &UserSystem::onUserDeleteAction);
    connect(ui.RemainingTicketsSearchToolAction, &QAction::triggered, this, &UserSystem::onRemainingTicketsSearchToolAction);
    connect(ui.PurchaseTicketsToolAction, &QAction::triggered, this, &UserSystem::onPurchaseTicketsToolAction);
    connect(ui.OrderHistorySearchToolAction, &QAction::triggered, this, &UserSystem::onOrderHistorySearchToolAction);
    connect(ui.TicketRefundOrChangeToolAction, &QAction::triggered, this, &UserSystem::onTicketRefundOrChangeToolAction);
}

void UserSystem::onUserInformationAction()
{
    UserInformation* userInfoDialog = new UserInformation(this);
    userInfoDialog->SetupUserInformation(currentUser);
    userInfoDialog->setWindowTitle("用户信息");
    userInfoDialog->setWindowModality(Qt::ApplicationModal);
    userInfoDialog->show();
}

void UserSystem::onLogoutAction()
{
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, "Logout", "你是否确定要退出登录？",
        QMessageBox::Yes | QMessageBox::No);
    if (reply == QMessageBox::Yes) {
        this->close();
        LoginSystem* loginWindow = new LoginSystem();
        loginWindow->initLoginSystem();
        loginWindow->show();
    }
}

void UserSystem::onUserDeleteAction()
{
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, "注销用户", "确定要注销当前用户吗？此操作不可恢复！",
        QMessageBox::Yes | QMessageBox::No);
    if (reply == QMessageBox::Yes) {
        // 从用户队列中删除当前用户
        auto& queue = allData->usersQueue;
        queue.erase(std::remove_if(queue.begin(), queue.end(), [this](const users& u) {
            return u.getUserId() == currentUser.getUserId();
        }), queue.end());
        allData->User_WriteToFile(USER_INFORMATION_FILE_POSITION);
        QMessageBox::information(this, "注销成功", "用户已注销，系统将返回登录界面。");
        this->close();
        LoginSystem* loginWindow = new LoginSystem();
        loginWindow->initLoginSystem();
        loginWindow->show();
    }
}

void UserSystem::onRemainingTicketsSearchToolAction()
{
	purchaseTicketSearchWindow = new Purchase_Ticket_Search(this,true); // 创建余票查询界面
	purchaseTicketSearchWindow->setWindowTitle("余票查询");
	purchaseTicketSearchWindow->setWindowModality(Qt::ApplicationModal); // 设置为应用程序模态对话框
	purchaseTicketSearchWindow->show(); // 显示余票查询界面
	for (auto it = allData->stationsQueue.begin(); it != allData->stationsQueue.end(); it++) {
		purchaseTicketSearchWindow->ui.start_station->addItem(QString::fromStdString(it->getName())); // 添加车站到下拉框
		purchaseTicketSearchWindow->ui.end_station->addItem(QString::fromStdString(it->getName())); // 添加车站到下拉框
	}
}

void UserSystem::onPurchaseTicketsToolAction()
{
	purchaseTicketSearchWindow = new Purchase_Ticket_Search(this,false); // 创建购票搜索界面
	purchaseTicketSearchWindow->setWindowTitle("购票");
	purchaseTicketSearchWindow->setWindowModality(Qt::ApplicationModal); // 设置为应用程序模态对话框
	purchaseTicketSearchWindow->show(); // 显示购票搜索界面
	for (auto it = allData->stationsQueue.begin(); it != allData->stationsQueue.end(); it++) {
		purchaseTicketSearchWindow->ui.start_station->addItem(QString::fromStdString(it->getName())); // 添加车站到下拉框
		purchaseTicketSearchWindow->ui.end_station->addItem(QString::fromStdString(it->getName())); // 添加车站到下拉框
	}
}


void UserSystem::onOrderHistorySearchToolAction()
{
    OrderHistoryDialog* dlg = new OrderHistoryDialog(currentUser, this, false); // 不显示退票/改签按钮
    dlg->exec();
    delete dlg;
}


void UserSystem::onTicketRefundOrChangeToolAction()
{
    OrderHistoryDialog* dlg = new OrderHistoryDialog(currentUser, this, true); // 显示退票/改签按钮
    dlg->setWindowTitle("退票/改签"); // 设置窗口标题为“退票/改签”
    dlg->exec();
    delete dlg;
}


